package com.fingerth.myjsondemo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingerth.myjsondemo.PhotoGalleryRepository;
import com.fingerth.myjsondemo.PictureRepository;
import com.fingerth.myjsondemo.bean.GalleryBean;
import com.fingerth.myjsondemo.tab.PhotoGallery;
import com.fingerth.myjsondemo.tab.Picture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

@RestController
@RequestMapping("test")
public class GalleryController {

    @Autowired
    PhotoGalleryRepository pRepository;

    @Autowired
    PictureRepository picRepository;


    @PostMapping("addGallery")
    public String addGallery(HttpServletRequest request) {
        String galleryAuthorId = request.getParameter("galleryAuthorId");
        String subject = request.getParameter("gallerySubject");
        String message = request.getParameter("galleryMessage");
        String images = request.getParameter("galleryImages");
        String type = request.getParameter("galleryType");

        PhotoGallery p = new PhotoGallery(galleryAuthorId, subject, message, images, type);
        try {
            PhotoGallery x = pRepository.save(p);
            return "{\"result\":0,\"msg\":\"success\"}";
        } catch (Exception e) {
            return "{\"result\":1,\"msg\":\"error\"}";
        }
    }

    @PostMapping("getGallery")
    public String getGallery(HttpServletRequest request) {
        String type = request.getParameter("lstType");
        String pageSize = request.getParameter("pageSize");
        String pageIndex = request.getParameter("pageIndex");
        Page<PhotoGallery> list = pRepository.findAll(PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "id")));
//        Page<PhotoGallery> list = pRepository.findPhotoGalleriesByGalleryType(0,PageRequest.of(0,20));
//        List<PhotoGallery> list = pRepository.findAll(pageable);

        String relativelyPath=System.getProperty("user.dir")+"\\out\\production\\resources\\static\\image";
        System.out.println(relativelyPath);

        GalleryBean bean = new GalleryBean();
        for (PhotoGallery photoGallery : list) {
            String[] ims = photoGallery.galleryImages.split(",");
            List<Long> ids = new ArrayList<>();
            for (String im : ims) {
                try {
                    ids.add(Long.parseLong(im));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            List<Picture> pictures = picRepository.findPicturesByIdIn(ids);
            GalleryBean.PoGallery pg = new GalleryBean.PoGallery(photoGallery);
            if (pictures != null && pictures.size() > 0) {
                pg.w = pictures.get(0).picWith;
                pg.h = pictures.get(0).picHeight;
                pg.images = new String[pictures.size()];
                for (int c = 0; c < pictures.size(); c++) {
                    pg.images[c] = pictures.get(c).name;
                }
            }
            bean.list.add(pg);
        }
        try {
            return new ObjectMapper().writeValueAsString(bean);
        } catch (Exception e) {
            return "{\"result\":1,\"msg\":\"error\"}";
        }
    }

}
